package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Admin;
import cn.edu.ujn.shi.dao.AdminDto;
import cn.edu.ujn.shi.service.IAdminService;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.PrintWriter;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    IAdminService iAdminService;

    /**
     * 返回管理员列表页面
     *
     * @param request
     * @param response
     */
    @RequestMapping("/AListAdminServlet")
    public void AListAdminServlet(HttpServletRequest request, HttpServletResponse response) {//Java方法声明，它表示一个公共的无返回值的方法
        try {
            response.setContentType("text/html;charset=utf-8");//将HTTP响应的内容类型设置为HTML文本，并将字符编码设置为UTF-8
            String userName = request.getParameter("username");//从HTTP请求中获取名为"username"的参数值，并将其赋值给一个名为userName的字符串变量
            // 查找管理员列表
            List<Admin> list = iAdminService.getByUserName(userName);//调用名为iAdminService的服务层对象中的getByUserName方法，并将参数userName传入该方法中，获取返回的管理员列表，并将其赋值给一个名为list的List类型的变量

            PrintWriter out = response.getWriter();//获取名为response的HttpServletResponse对象中的输出流，并将其赋值给一个名为out的PrintWriter类型的变量
            //引用jsp文件
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header-admin.jsp");//获取名为request的HttpServletRequest对象中的请求转发器，并将其赋值给一个名为headerDispatcher的RequestDispatcher类型的变量
            headerDispatcher.include(request, response);
            out.println("<center><img src='images/权限.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>管理员账号管理</b></font></center>");
            out.println("<hr/>");


            out.println("<br/>");
            out.println("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='AddAdmin'><img src='images/add.png' width='100px'/></a>");
            out.println("<center>");
            out.println("    <form action='AListAdminServlet' method='get'>\n" +
                    "        <label for='username'>按用户名搜索：</label>\n" +
                    "        <input type='text' id='username' name='username'>\n" +
                    "        <button type='submit'>搜索</button>\n" +
                    "    </form>");
            out.println("<table border=1 width=90% >");
            out.println("<tr bgcolor='#E0EEE0'>");
            out.println("<td width='200px'><b>用户名</b></td ><td width='200px'><b>密码</b></td><td width='200px'><b>操作</b></td>");
            out.println("</tr>");

            for (Admin u : list) {
                out.println("<tr>");
                out.println("<td width='200px'>");
                out.println(u.getUsername());
                out.println("</td>");
                out.println("<td width='200px'>");
                out.println(u.getUserpass());
                out.println("</td>");
                out.println("<td width='200px'>");
                out.println("<a href='AddAdmin?username=" + u.getUsername() + "&userpass=" + u.getUserpass() + "'>修改管理员</a>");
                out.println("<a href='DeleteAdminServlet?username=" + u.getUsername() + "'>删除管理员</a>");

                out.println("</td>");
                out.println("</tr>");
            }

            out.println("</table></center>");

            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/footer.jsp");
            headerDispatcher1.include(request, response);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除管理员请求
     *
     * @param request
     * @param response
     */
    @RequestMapping("/DeleteAdminServlet")
    public void deleteAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            String username =request.getParameter("username");
            iAdminService.delete(username);

            response.sendRedirect("AListAdminServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增管理员
     *
     * @param request
     * @param response
     */
    @RequestMapping("/InsertAdminServlet")
    public void insertAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();

            request.setCharacterEncoding("utf-8");

            // 从请求中获取入参
            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");

            if (username.equalsIgnoreCase("") || userpass.equalsIgnoreCase("")) {
                out.println("<script language = javascript>alert('WARNING')");
                out.println("window.history.go(-1)</script>");
            } else {
                // 判断重复
                List<Admin> list = iAdminService.getByUserName(username);
                try {
                    if (!list.isEmpty()) {
                        out.println("<script language = javascript>alert('用户重复!')");
                        out.println("window.history.go(-1)</script>");
                    } else {

                        // 没有重复新增
                        iAdminService.insert(new Admin(username, userpass));
                        out.println("<script language = javascript>alert('SUCCESS!')");
                        response.sendRedirect("AListAdminServlet");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/UpdateAdminServlet")
    public void UpdateAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();

            request.setCharacterEncoding("utf-8");

            // 从请求中获取入参
            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");
            String username_old = request.getParameter("username_old");

            if (username.equalsIgnoreCase("") || userpass.equalsIgnoreCase("")) {
                out.println("<script language = javascript>alert('WARNING')");
                out.println("window.history.go(-1)</script>");
            } else {
                // 判断重复
                List<Admin> list = iAdminService.getByUserName(username);
                try {
                    if (!username.equals(username_old) && !list.isEmpty()) {
                        out.println("<script l@Controller\n" +
                                "public class AdminController {\n" +
                                "    @Autowired\n" +
                                "    IAdminService iAdminService;\n" +
                                "\n" +
                                "    /**\n" +
                                "     * 返回管理员列表页面\n" +
                                "     *\n" +
                                "     * @param request\n" +
                                "     * @param response\n" +
                                "     */\n" +
                                "    @RequestMapping(\"/AListAdminServlet\")\n" +
                                "    public void AListAdminServlet(HttpServletRequest request, HttpServletResponse response) {\n" +
                                "        try {\n" +
                                "            response.setContentType(\"text/html;charset=utf-8\");\n" +
                                "            String userName = request.getParameter(\"username\");\n" +
                                "            // 查找管理员列表\n" +
                                "            List<Admin> list = iAdminService.getByUserName(userName);\n" +
                                "\n" +
                                "            PrintWriter out = response.getWriter();\n" +
                                "            //引用jsp文件\n" +
                                "            RequestDispatcher headerDispatcher = request.getRequestDispatcher(\"/WEB-INF/jsp/header-admin.jsp\");\n" +
                                "            headerDispatcher.include(request, response);\n" +
                                "            out.println(\"<center><img src='images/权限.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>管理员账号管理</b></font></center>\");\n" +
                                "            out.println(\"<hr/>\");\n" +
                                "\n" +
                                "\n" +
                                "            out.println(\"<br/>\");\n" +
                                "            out.println(\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='AddAdmin'><img src='images/add.png' width='100px'/></a>\");\n" +
                                "            out.println(\"<center>\");\n" +
                                "            out.println(\"    <form action='AListAdminServlet' method='get'>\\n\" +\n" +
                                "                    \"        <label for='username'>按用户名搜索：</label>\\n\" +\n" +
                                "                    \"        <input type='text' id='username' name='username'>\\n\" +\n" +
                                "                    \"        <button type='submit'>搜索</button>\\n\" +\n" +
                                "                    \"    </form>\");\n" +
                                "            out.println(\"<table border=1 width=90% >\");\n" +
                                "            out.println(\"<tr bgcolor='#E0EEE0'>\");\n" +
                                "            out.println(\"<td width='200px'><b>用户名</b></td ><td width='200px'><b>密码</b></td><td width='200px'><b>操作</b></td>\");\n" +
                                "            out.println(\"</tr>\");\n" +
                                "\n" +
                                "            for (Admin u : list) {\n" +
                                "                out.println(\"<tr>\");\n" +
                                "                out.println(\"<td width='200px'>\");\n" +
                                "                out.println(u.getUsername());\n" +
                                "                out.println(\"</td>\");\n" +
                                "                out.println(\"<td width='200px'>\");\n" +
                                "                out.println(u.getUserpass());\n" +
                                "                out.println(\"</td>\");\n" +
                                "                out.println(\"<td width='200px'>\");\n" +
                                "                out.println(\"<a href='AddAdmin?username=\" + u.getUsername() + \"&userpass=\" + u.getUserpass() + \"'>修改管理员</a>\");\n" +
                                "                out.println(\"<a href='DeleteAdminServlet?username=\" + u.getUsername() + \"'>删除管理员</a>\");\n" +
                                "\n" +
                                "                out.println(\"</td>\");\n" +
                                "                out.println(\"</tr>\");\n" +
                                "            }\n" +
                                "\n" +
                                "            out.println(\"</table></center>\");\n" +
                                "\n" +
                                "            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher(\"/WEB-INF/jsp/footer.jsp\");\n" +
                                "            headerDispatcher1.include(request, response);\n" +
                                "            out.flush();\n" +
                                "            out.close();\n" +
                                "        } catch (Exception e) {\n" +
                                "            e.printStackTrace();\n" +
                                "        }\n" +
                                "    }\n" +
                                "\n" +
                                "    /**\n" +
                                "     * 删除管理员请求\n" +
                                "     *\n" +
                                "     * @param request\n" +
                                "     * @param response\n" +
                                "     */\n" +
                                "    @RequestMapping(\"/DeleteAdminServlet\")\n" +
                                "    public void deleteAdminServlet(HttpServletRequest request, HttpServletResponse response) {\n" +
                                "        try {\n" +
                                "            response.setContentType(\"text/html;charset=utf-8\");\n" +
                                "            request.setCharacterEncoding(\"utf-8\");\n" +
                                "            String username =request.getParameter(\"username\");\n" +
                                "            iAdminService.delete(username);\n" +
                                "\n" +
                                "            response.sendRedirect(\"AListAdminServlet\");\n" +
                                "        } catch (Exception e) {\n" +
                                "            e.printStackTrace();\n" +
                                "        }\n" +
                                "    }\n" +
                                "\n" +
                                "    /**\n" +
                                "     * 新增管理员\n" +
                                "     *\n" +
                                "     * @param request\n" +
                                "     * @param response\n" +
                                "     */\n" +
                                "    @RequestMapping(\"/InsertAdminServlet\")\n" +
                                "    public void insertAdminServlet(HttpServletRequest request, HttpServletResponse response) {\n" +
                                "        try {\n" +
                                "            response.setContentType(\"text/html;charset=utf-8\");\n" +
                                "            PrintWriter out = response.getWriter();\n" +
                                "\n" +
                                "            request.setCharacterEncoding(\"utf-8\");\n" +
                                "\n" +
                                "            // 从请求中获取入参\n" +
                                "            String username = request.getParameter(\"username\");\n" +
                                "            String userpass = request.getParameter(\"userpass\");\n" +
                                "\n" +
                                "            if (username.equalsIgnoreCase(\"\") || userpass.equalsIgnoreCase(\"\")) {\n" +
                                "                out.println(\"<script language = javascript>alert('WARNING')\");\n" +
                                "                out.println(\"window.history.go(-1)</script>\");\n" +
                                "            } else {\n" +
                                "                // 判断重复\n" +
                                "                List<Admin> list = iAdminService.getByUserName(username);\n" +
                                "                try {\n" +
                                "                    if (!list.isEmpty()) {\n" +
                                "                        out.println(\"<script language = javascript>alert('用户重复!')\");\n" +
                                "                        out.println(\"window.history.go(-1)</script>\");\n" +
                                "                    } else {\n" +
                                "\n" +
                                "                        // 没有重复新增\n" +
                                "                        iAdminService.insert(new Admin(username, userpass));\n" +
                                "                        out.println(\"<script language = javascript>alert('SUCCESS!')\");\n" +
                                "                        response.sendRedirect(\"AListAdminServlet\");\n" +
                                "                    }\n" +
                                "\n" +
                                "                } catch (Exception e) {\n" +
                                "                    // TODO Auto-generated catch block\n" +
                                "                    e.printStackTrace();\n" +
                                "                }\n" +
                                "            }\n" +
                                "        } catch (Exception e) {\n" +
                                "            e.printStackTrace();\n" +
                                "        }\n" +
                                "    }\n" +
                                "\n" +
                                "    @RequestMapping(\"/UpdateAdminServlet\")\n" +
                                "    public void UpdateAdminServlet(HttpServletRequest request, HttpServletResponse response) {\n" +
                                "        try {\n" +
                                "            response.setContentType(\"text/html;charset=utf-8\");\n" +
                                "            PrintWriter out = response.getWriter();\n" +
                                "\n" +
                                "            request.setCharacterEncoding(\"utf-8\");\n" +
                                "\n" +
                                "            // 从请求中获取入参\n" +
                                "            String username = request.getParameter(\"username\");\n" +
                                "            String userpass = request.getParameter(\"userpass\");\n" +
                                "            String username_old = request.getParameter(\"username_old\");\n" +
                                "\n" +
                                "            if (username.equalsIgnoreCase(\"\") || userpass.equalsIgnoreCase(\"\")) {\n" +
                                "                out.println(\"<script language = javascript>alert('WARNING')\");\n" +
                                "                out.println(\"window.history.go(-1)</script>\");\n" +
                                "            } else {\n" +
                                "                // 判断重复\n" +
                                "                List<Admin> list = iAdminService.getByUserName(username);\n" +
                                "                try {\n" +
                                "                    if (!username.equals(username_old) && !list.isEmpty()) {\n" +
                                "                        out.println(\"<script language = javascript>alert('用户重复!')\");\n" +
                                "                        out.println(\"window.history.go(-1)</script>\");\n" +
                                "                    } else {\n" +
                                "\n" +
                                "                        // 没有重复新增\n" +
                                "                        iAdminService.updateAdmin(new AdminDto(username, userpass, username_old));\n" +
                                "                        out.println(\"<script language = javascript>alert('SUCCESS!')\");\n" +
                                "                        response.sendRedirect(\"AListAdminServlet\");\n" +
                                "                    }\n" +
                                "\n" +
                                "                } catch (Exception e) {\n" +
                                "                    // TODO Auto-generated catch block\n" +
                                "                    e.printStackTrace();\n" +
                                "                }\n" +
                                "            }\n" +
                                "        } catch (Exception e) {\n" +
                                "            e.printStackTrace();\n" +
                                "        }\n" +
                                "    }\n" +
                                "\n" +
                                "\n" +
                                "}anguage = javascript>alert('用户重复!')");
                        out.println("window.history.go(-1)</script>");
                    } else {

                        // 没有重复新增
                        iAdminService.updateAdmin(new AdminDto(username, userpass, username_old));
                        out.println("<script language = javascript>alert('SUCCESS!')");
                        response.sendRedirect("AListAdminServlet");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}